<?php
/**
 * Class that operate on table 'hs_image'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2012-05-30 11:06
 */
class HsImageMySqlDAO implements HsImageDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return HsImageMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM hs_image WHERE imageID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM hs_image';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM hs_image ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param hsImage primary key
 	 */
	public function delete($imageID){
		$sql = 'DELETE FROM hs_image WHERE imageID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($imageID);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param HsImageMySql hsImage
 	 */
	public function insert($hsImage){
		$sql = 'INSERT INTO hs_image (albumID, imageTitle, imageDescription, imageTimestamp) VALUES (?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsImage->albumID);
		$sqlQuery->set($hsImage->imageTitle);
		$sqlQuery->set($hsImage->imageDescription);
		$sqlQuery->set($hsImage->imageTimestamp);

		$id = $this->executeInsert($sqlQuery);	
		$hsImage->imageID = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param HsImageMySql hsImage
 	 */
	public function update($hsImage){
		$sql = 'UPDATE hs_image SET albumID = ?, imageTitle = ?, imageDescription = ?, imageTimestamp = ? WHERE imageID = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsImage->albumID);
		$sqlQuery->set($hsImage->imageTitle);
		$sqlQuery->set($hsImage->imageDescription);
		$sqlQuery->set($hsImage->imageTimestamp);

		$sqlQuery->set($hsImage->imageID);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM hs_image';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByAlbumID($value){
		$sql = 'SELECT * FROM hs_image WHERE albumID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByImageTitle($value){
		$sql = 'SELECT * FROM hs_image WHERE imageTitle = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByImageDescription($value){
		$sql = 'SELECT * FROM hs_image WHERE imageDescription = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByImageTimestamp($value){
		$sql = 'SELECT * FROM hs_image WHERE imageTimestamp = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByAlbumID($value){
		$sql = 'DELETE FROM hs_image WHERE albumID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByImageTitle($value){
		$sql = 'DELETE FROM hs_image WHERE imageTitle = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByImageDescription($value){
		$sql = 'DELETE FROM hs_image WHERE imageDescription = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByImageTimestamp($value){
		$sql = 'DELETE FROM hs_image WHERE imageTimestamp = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return HsImageMySql 
	 */
	protected function readRow($row){
		$hsImage = new HsImage();
		
		$hsImage->imageID = $row['imageID'];
		$hsImage->albumID = $row['albumID'];
		$hsImage->imageTitle = $row['imageTitle'];
		$hsImage->imageDescription = $row['imageDescription'];
		$hsImage->imageTimestamp = $row['imageTimestamp'];

		return $hsImage;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return HsImageMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>